package com.qf.ssm.controller;

import cn.hutool.http.HttpResponse;
import com.qf.ssm.entity.order.OrderMessage;
import com.qf.ssm.entity.order.UserAddress;
import com.qf.ssm.entity.user.*;
import com.qf.ssm.service.UploadService;
import com.qf.ssm.service.UserService;
import com.qf.ssm.utils.FileUtils;
import com.qf.ssm.utils.ResponseResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Author wzy
 * @Date 0015 2021-02-15 17:36
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @Resource
    UploadService uploadService;

    @PostMapping("/upload/headimg")
    public ResponseResult<String> upload(@RequestParam("img") MultipartFile multipartFile) throws IOException {
        //  upload/images/20210204/1.png
        String savePath = uploadService.upload(multipartFile);

        //数据库保存的操作
        return ResponseResult.success(FileUtils.ROOT_PATH + savePath);
    }

    /**
     * 管理员登录方法
     *
     * @param admin
     * @return
     */
    @PostMapping("/login/admin")
    public ResponseResult<Admin> adminLogin(@RequestBody Admin admin, HttpServletRequest httpServletRequest) {
        ResponseResult<Admin> result = userService.adminLogin(admin);

        if (result != null) {
            HttpSession session = httpServletRequest.getSession();

            session.setAttribute("username", admin.getUsername());

            System.out.println(session.getAttribute("username"));
        }
        return result;
    }

    /**
     * 普通用户登陆方法
     *
     * @param user
     * @return
     */
    @PostMapping("/login/user")
    public ResponseResult<User> userLogin(@RequestBody User user, HttpServletRequest httpServletRequest) {
        ResponseResult<User> result = userService.userLogin(user);
        if (result != null) {
            HttpSession session = httpServletRequest.getSession();
            session.setAttribute("username", user.getUsername());
        }
        return result;
    }

    /**
     * 查询普通用户信息
     *
     * @return
     */
    @GetMapping("/userallselect")
    public ResponseResult<List<User>> userAllSelect() {
        ResponseResult<List<User>> result = userService.userAllSelect();
        return result;
    }

    /**
     * 查询VIP用户信息
     *
     * @return
     */
    @GetMapping("/vipuserallselect")
    public ResponseResult<List<User>> vipuserAllSelect() {
        ResponseResult<List<User>> result = userService.vipuserAllSelect();
        return result;
    }

    /**
     * 添加普通用户信息
     *
     * @param user
     * @return
     */
    @PostMapping("/user/insert")
    public ResponseResult<Integer> userInsert(@RequestBody User user) {
        ResponseResult<Integer> resultCount = userService.userInsert(user);
        return resultCount;
    }

    /**
     * 添加VIP用户信息
     *
     * @param user
     * @return
     */
    @PostMapping("/vipuser/insert")
    public ResponseResult<Integer> vipuserInsert(@RequestBody User user) {
        ResponseResult<Integer> resultCount = userService.vipuserInsert(user);
        return resultCount;
    }

    /**
     * 查询VIP等级信息
     *
     * @return
     */
    @GetMapping("/viplevellselect")
    public ResponseResult<List<VipLevel>> vipLevelSelect() {
        ResponseResult<List<VipLevel>> result = userService.vipLevelSelect();
        return result;
    }

    /**
     * 添加会员等级
     *
     * @param vipLevel
     * @return
     */
    @PostMapping("/viplevel/insert")
    public ResponseResult<Integer> viplevelInsert(@RequestBody VipLevel vipLevel) {
        ResponseResult<Integer> resultCount = userService.viplevelInsert(vipLevel);
        return resultCount;
    }

    /**
     * 删除VIP等级信息
     *
     * @param id
     * @return
     */
    @GetMapping("/viplevel/delete")
    public ResponseResult<Integer> viplevelDelete(int id) {
        ResponseResult<Integer> resultCount = userService.viplevelDelete(id);
        return resultCount;
    }

    /**
     * 修改VIP等级信息
     *
     * @param vipLevel
     * @return
     */
    @PostMapping("/viplevel/update")
    public ResponseResult<Integer> viplevelUpdate(@RequestBody VipLevel vipLevel) {
        ResponseResult<Integer> resultCount = userService.viplevelUpdate(vipLevel);
        return resultCount;
    }

    /**
     * 删除普通用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/user/delete")
    public ResponseResult<Integer> userDelete(int id) {
        ResponseResult<Integer> resultCount = userService.userDelete(id);
        return resultCount;
    }

    /**
     * 删除VIP用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/vipuser/delete")
    public ResponseResult<Integer> vipuserDelete(int id) {
        ResponseResult<Integer> resultCount = userService.vipuserDelete(id);
        return resultCount;
    }

    /**
     * 修改普通用户信息
     *
     * @param user
     * @return
     */
    @PostMapping("/user/modify")
    public ResponseResult<Integer> userModify(@RequestBody User user) {
        ResponseResult<Integer> resultCount = userService.userModify(user);
        return resultCount;
    }

    /**
     * 修改VIP用户信息
     *
     * @param user
     * @return
     */
    @PostMapping("/vipuser/modify")
    public ResponseResult<Integer> vipuserModify(@RequestBody User user) {
        ResponseResult<Integer> resultCount = userService.vipuserModify(user);
        return resultCount;
    }

    /**
     * 查询成长模型设置表信息
     *
     * @return
     */
    @GetMapping("/growthlselect")
    public ResponseResult<List<GrowthSettings>> growthSettingsSelect() {
        ResponseResult<List<GrowthSettings>> result = userService.growthSettingsSelect();
        return result;
    }

    /**
     * 添加成长模型设置表信息
     *
     * @param growthSettings
     * @return
     */
    @PostMapping("/growthSettings/insert")
    public ResponseResult<Integer> growthSettingsInsert(@RequestBody GrowthSettings growthSettings) {
        ResponseResult<Integer> resultCount = userService.growthSettingsInsert(growthSettings);
        return resultCount;
    }

    /**
     * 修改成长模型设置表信息
     *
     * @param growthSettings
     * @return
     */
    @PostMapping("/growthSettings/update")
    public ResponseResult<Integer> growthSettingsUpdate(@RequestBody GrowthSettings growthSettings) {
        ResponseResult<Integer> resultCount = userService.growthSettingsUpdate(growthSettings);
        return resultCount;
    }

    /**
     * 删除成长模型设置表信息
     *
     * @param id
     * @return
     */
    @PostMapping("/growthSettings/delete")
    public ResponseResult<Integer> growthSettingsDelete(int id) {
        ResponseResult<Integer> resultCount = userService.growthSettingsDelete(id);
        return resultCount;
    }

    /**
     * 查询成长模型设置表ID和项目名
     * 查询成长模型设置表ID，用于添加模型金额设置表时的填写的SetID
     *
     * @return
     */
    @GetMapping("/growthIDselect")
    public ResponseResult<List<GrowthSettings>> growthSettingsIDSelect() {
        ResponseResult<List<GrowthSettings>> result = userService.growthSettingsIDSelect();
        return result;
    }

    @GetMapping("/userResourceselect")
    public ResponseResult<List<UserResource>> userResourceSelect() {
        ResponseResult<List<UserResource>> result = userService.userResourceSelect();
        return result;
    }

    /**
     * 查询成长模型设置表信息
     *
     * @return
     */
    @GetMapping("/monysetting/select")
    public ResponseResult<List<MoneySettings>> moneySettingsSelect() {
        ResponseResult<List<MoneySettings>> result = userService.moneySettingsSelect();
        return result;
    }

    /**
     * 添加成长模型设置表信息
     *
     * @param moneySettings
     * @return
     */
    @PostMapping("/monysetting/insert")
    public ResponseResult<Integer> moneySettingsInsert(@RequestBody MoneySettings moneySettings) {
        ResponseResult<Integer> resultCount = userService.moneySettingsInsert(moneySettings);
        return resultCount;
    }

    /**
     * 修改成长模型设置表信息
     *
     * @param moneySettings
     * @return
     */
    @PostMapping("/monysetting/update")
    public ResponseResult<Integer> moneySettingsUpdate(@RequestBody MoneySettings moneySettings) {
        ResponseResult<Integer> resultCount = userService.moneySettingsUpdate(moneySettings);
        return resultCount;
    }

    /**
     * 删除成长模型设置表信息
     *
     * @param id
     * @return
     */
    @PostMapping("/monysetting/delete")
    public ResponseResult<Integer> moneySettingsDelete(int id) {
        ResponseResult<Integer> resultCount = userService.moneySettingsDelete(id);
        return resultCount;
    }

    /**
     * 动态限制查询普通用户信息
     *
     * @param userDynamicQuery
     * @return
     */
    @PostMapping("/user/dynamicQuery")
    public ResponseResult<List<User>> userDynamicQuery(@RequestBody UserDynamicQuery userDynamicQuery) {
        ResponseResult<List<User>> result = userService.userDynamicQuery(userDynamicQuery);
        return result;
    }

    /**
     * 动态限制查询VIP会员信息
     *
     * @param vipuserDynamicQuery
     * @return
     */
    @PostMapping("/vipuser/dynamicQuery")
    public ResponseResult<List<User>> vipuserDynamicQuery(@RequestBody VipuserDynamicQuery vipuserDynamicQuery) {
        ResponseResult<List<User>> result = userService.vipuserDynamicQuery(vipuserDynamicQuery);
        return result;
    }


    ////////////////////////////////   rmf模型设置表

    /**
     * 查询成长模型设置表信息
     *
     * @return
     */
    @GetMapping("/rmf/select")
    public ResponseResult<List<RmfModelSettings>> rmfSelect() {
        ResponseResult<List<RmfModelSettings>> result = userService.rmfSelect();
        return result;
    }

    /**
     * 添加成长模型设置表信息
     *
     * @param rmfModelSettings
     * @return
     */
    @PostMapping("/rmf/insert")
    public ResponseResult<Integer> rmfInsert(@RequestBody RmfModelSettings rmfModelSettings) {
        ResponseResult<Integer> resultCount = userService.rmfInsert(rmfModelSettings);
        return resultCount;
    }

    /**
     * 修改成长模型设置表信息
     *
     * @param rmfModelSettings
     * @return
     */
    @PostMapping("/rmf/update")
    public ResponseResult<Integer> rmfUpdate(@RequestBody RmfModelSettings rmfModelSettings) {
        ResponseResult<Integer> resultCount = userService.rmfUpdate(rmfModelSettings);
        return resultCount;
    }

    /**
     * 删除成长模型设置表信息
     *
     * @param id
     * @return
     */
    @PostMapping("/rmf/delete")
    public ResponseResult<Integer> rmfDelete(int id) {
        ResponseResult<Integer> resultCount = userService.rmfDelete(id);
        return resultCount;
    }

    /**
     * 查询用户订单简要信息
     *
     * @return
     */
    @GetMapping("/user/orderselect")
    public ResponseResult<List<OrderMessage>> orderSelect(int uid) {
        ResponseResult<List<OrderMessage>> result = userService.orderSelect(uid);
        return result;
    }

    /**
     * 查询用户订单详情信息
     *
     * @return
     */
    @GetMapping("/user/pointsdetailSelect")
    public ResponseResult<List<Points_details>> pointsDetailSelect(int uid) {
        ResponseResult<List<Points_details>> result = userService.pointsDetailSelect(uid);
        return result;
    }

    /**
     * 查询配送地址
     *
     * @param uid
     * @return
     */
    @GetMapping("/user/addrselect")
    public ResponseResult<List<UserAddress>> addrSelect(int uid) {
        ResponseResult<List<UserAddress>> result = userService.addrSelect(uid);
        return result;
    }
}
